package ui;


import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.*;

import core.MenuItemType;
import ui.handlers.MenuHandler;

import java.rmi.RemoteException;

public class EditMenuScreen extends BaseScreen{

	private JFrame mainFrame;
	private JTextField itemName_textfield, itemPrice_textfield;
	private JTextArea itemDesc_textarea;
	private JComboBox<MenuItemType> itemType_combobox;
	private JButton addItem_btn, clear_btn;
	
	private JPanel mainContent_panel;
	private JScrollPane mainContent_scroll;
	
	private MenuHandler mHandler;
	
	/* Methods needed by the handler */
	
	public String getItemNameText()
	{ return itemName_textfield.getText(); }
	
	public String getItemDescText()
	{ return itemDesc_textarea.getText(); }
	
	public String getItemPriceText()
	{ return itemPrice_textfield.getText(); }
	
	public MenuItemType getItemType()
	{
		return (MenuItemType) itemType_combobox.getSelectedItem();
	}
	
	public void cleartItemNameText()
	{ itemName_textfield.setText(""); }
	
	public void clearItemDescText()
	{ itemDesc_textarea.setText(""); }
	
	public void clearItemPriceText()
	{ itemPrice_textfield.setText(""); }
	
	public void setErrorText(String s)
	{ itemDesc_textarea.setText(s); }
	
	public JButton getAddItemButton()
	{ return addItem_btn; }
	
	public JButton getClearButton()
	{ return clear_btn; }
	
	public JPanel getMainContent()
	{ return mainContent_panel; }
	
	public JScrollPane getMainScorllPane()
	{ return mainContent_scroll; }
	
	public JFrame getMainFrame()
	{ return mainFrame; }
	
	/* END OF HANDLER METHODS */
	
	public EditMenuScreen()
	{
		this.mainContent_panel = null;
		this.mainContent_scroll = null;
		mHandler = new MenuHandler(this);
		mainFrame = new JFrame();
		mainFrame.setTitle("Manager: Create and Edit the Menu");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(800, 650);
		mainFrame.setLayout(new BorderLayout());
		doTitle();
		this.loadMainContent();
		doAddItemSection();
		mainFrame.setVisible(true);
	}
	
	public void loadMainContent(){
		if(mainContent_scroll != null){
			mainFrame.remove(mainContent_scroll);
		}
		try {
			doMenu();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void doTitle()
	{
		String title = "<HTML><FONT SIZE = \"5\">" +
				"Menu</FONT></HTML>";
		JLabel title_lbl = new JLabel(title);
		mainFrame.add(title_lbl, BorderLayout.NORTH);
	}
	
	private void doMenu() throws RemoteException
	{
		
		mainContent_panel = new JPanel();
		mainContent_panel.validate();
		
		//added 2 for the daily special (1 for the label, 1 for the item info
		int itemSlots = store.getNumberMenuItems() + store.getMenuItemTypeValues().length + 2;
		mainContent_panel.setLayout(new GridLayout(itemSlots, 1));
		
		/* Making the daily special panels */
		JLabel special_lbl = new JLabel("<HTML><FONT SIZE = \"5\">Daily Special</FONT></HTML>");
		JLabel specialName_lbl = new JLabel(store.getDailySpecialName());
		JLabel specialPrice_lbl = new JLabel(store.getDailySpecialPrice());
		JLabel specialDesc_lbl = new JLabel(store.getDailySpecialDescription());
		
		mainContent_panel.add(new JPanel().add(special_lbl));
		
		JPanel special_panel = new JPanel();
		JPanel specialItem_panel = new JPanel();
		
		special_panel.setLayout(new GridLayout(1, 2));
		specialItem_panel.setLayout(new GridLayout(2, 1));
		specialItem_panel.setBorder(new EmptyBorder(0, 0, 10, 0));
		
		specialItem_panel.add(specialName_lbl);
		specialItem_panel.add(specialDesc_lbl);
		
		special_panel.add(specialItem_panel);
		special_panel.add(new JPanel().add(specialPrice_lbl));
		
		mainContent_panel.add(special_panel);
		/* Done adding the daily special */
		
		for(MenuItemType type : store.getMenuItemTypeValues()){
			JLabel typeLabel = new JLabel("<HTML><FONT SIZE = \"5\">" + type + "</FONT></HTML>");
			JPanel typePanel = new JPanel();
			typePanel.add(typeLabel);
			mainContent_panel.add(typePanel);
			
			for(int itemId : store.getMenuItemsByType(type)){
				
				JPanel temp_panel = new JPanel();
				temp_panel.setLayout(new GridLayout(1, 3));
				JPanel item_panel = new JPanel();
				item_panel.setLayout(new GridLayout(2, 1));
				item_panel.setBorder(new EmptyBorder(0, 0, 10, 0));
				
				JLabel name_lbl = new JLabel(store.getMenuItemName(itemId));
				JLabel desc_lbl = new JLabel(store.getMenuItemDescription(itemId));
				
				item_panel.add(name_lbl);
				item_panel.add(desc_lbl);
				
				JLabel price_lbl = new JLabel(store.getMenuItemPrice(itemId));
				JPanel price_panel = new JPanel();
				price_panel.add(price_lbl);
				
				JButton remove_btn = new JButton("Remove");
				remove_btn.addActionListener(new RemoveHandler(itemId, temp_panel, this));
				
				//add all components into one-line container
				temp_panel.add(item_panel);
				temp_panel.add(price_panel);
				temp_panel.add(remove_btn);
				
				mainContent_panel.add(temp_panel);
			}
		}
		
		mainContent_panel.validate();
		mainContent_scroll = new JScrollPane(mainContent_panel);
		mainContent_scroll.validate();
		mainFrame.add(mainContent_scroll, BorderLayout.CENTER);
		mainFrame.validate();
	}
	
	private void doAddItemSection()
	{
		itemName_textfield = new JTextField(10);
		itemPrice_textfield = new JTextField(10);
		itemDesc_textarea = new JTextArea(3, 20);
		
		try {
			itemType_combobox = new JComboBox<MenuItemType>(store.getMenuItemTypeValues());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JLabel itemName_lbl = new JLabel("Name:");
		JLabel itemType_lbl = new JLabel("Type:");
		JLabel itemPrice_lbl = new JLabel("Price:");
		JLabel itemDesc_lbl = new JLabel("Description");
		
		JPanel itemName_panel = new JPanel();
		itemName_panel.setLayout(new FlowLayout());
		itemName_panel.add(itemName_lbl);
		itemName_panel.add(itemName_textfield);
		
		JPanel itemType_panel = new JPanel();
		itemType_panel.setLayout(new FlowLayout());
		itemType_panel.add(itemType_lbl);
		itemType_panel.add(itemType_combobox);
		
		JPanel itemPrice_panel = new JPanel();
		itemPrice_panel.setLayout(new FlowLayout());
		itemPrice_panel.add(itemPrice_lbl);
		itemPrice_panel.add(itemPrice_textfield);
		
		JPanel itemDesc_panel = new JPanel();
		itemDesc_panel.setLayout(new BorderLayout());
		itemDesc_panel.add(itemDesc_lbl, BorderLayout.NORTH);
		itemDesc_panel.add(itemDesc_textarea, BorderLayout.CENTER);
		
		JPanel itemInfo_panel = new JPanel();
		itemInfo_panel.setLayout(new GridLayout(1, 3));
		itemInfo_panel.add(itemName_panel);
		itemInfo_panel.add(itemType_panel);
		itemInfo_panel.add(itemPrice_panel);
		
		JPanel addItem_panel = new JPanel();
		addItem_panel.setLayout(new GridLayout(3, 1));
		addItem_panel.setBorder(new EmptyBorder(0, 0, 15, 0));
		addItem_panel.add(new JLabel("Add an item:"));
		addItem_panel.add(itemInfo_panel);
		addItem_panel.add(itemDesc_panel);
		
		addItem_btn = new JButton("Add Item");
		addItem_btn.addActionListener(mHandler);
		
		clear_btn = new JButton("Clear");
		clear_btn.addActionListener(mHandler);
		
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new FlowLayout());
		button_panel.setBorder(new EmptyBorder(0, 10, 0, 0));
		button_panel.add(addItem_btn);
		button_panel.add(clear_btn);
		
		JPanel bottom_panel = new JPanel();
		bottom_panel.setLayout(new FlowLayout());
		bottom_panel.add(addItem_panel);
		bottom_panel.add(button_panel);
		
		mainFrame.add(bottom_panel, BorderLayout.SOUTH);
	}
}

class RemoveHandler implements ActionListener {

	private int id;
	private EditMenuScreen editMenuScreen;
	private JPanel panelToRemove;
	
	public RemoveHandler(int id, JPanel panelToRemove, EditMenuScreen editMenuScreen)
	{
		this.id = id;
		this.editMenuScreen = editMenuScreen;
		this.panelToRemove = panelToRemove;
	}
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		editMenuScreen.getMainContent().remove(panelToRemove);
		editMenuScreen.getMainContent().validate();
		editMenuScreen.getMainScorllPane().remove(editMenuScreen.getMainContent());
		editMenuScreen.getMainScorllPane().validate();
		editMenuScreen.getMainFrame().remove(editMenuScreen.getMainScorllPane());
		editMenuScreen.getMainFrame().validate();
		
		try {
			editMenuScreen.getStore().removeMenuItemById(id);
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		editMenuScreen.loadMainContent();
	}
}